package com.fusion.core.file.filedatabase;

/**
 * @author SwethaShivakumar 
 * Project: fusion
 * Date: Mar 12, 2009
 * URL: http://fusionwiki.co.cc
 * Mail Me At: shiva@fusionproject.co.cc
 */
public enum FileType {
    EVERYTHING("All Files", 0, new FileTypeIdentifier() {
        public boolean matches(String s) {
            return true;
        }
    }),
    AUDIO("Audio", 1, new ExtensionFileType(new String[] {"mp3", "mp4", "wav", "acc", "ogg", "asf", "wma", "aiff"})),
    ARCHIVE("Archives", 4, new ExtensionFileType(new String[] {"rar", "zip", "tar", "gz", "7z", "jar", "war", "ear"}) {
        public boolean matches(String s) {
            if (!super.matches(s)) {
                if (s.length() > 3 && s.charAt(s.length()-3) == 'r' && Character.isDigit(s.charAt(s.length()-2)) && Character.isDigit(s.charAt(s.length()-1)))
                    return true;
                return false;
            }
            return true;
        }
    }),
    CDDVD("CD/DVD Images", 3, new ExtensionFileType(new String[] {"iso", "img", "bin"})),
    DOCUMENT("Documents", 6, new ExtensionFileType(new String[] {"doc", "txt", "nfo", "pdf"})),
    IMAGE("Pictures", 5, new ExtensionFileType(new String[] {"jpg", "jpeg", "gif", "png", "bmp", "tiff"})),
    PRESENTATION("Presentations", 7, new ExtensionFileType(new String[] {"key", "pps", "ppt"})),
    VIDEO("Video", 2, new ExtensionFileType(new String[] {"avi", "mkv", "mpg", "mpeg", "mov", "asf", "wmv", "divx", "xvid", "rmvb", "rm"}));

    private final String description;
    private final byte id;
    private final FileTypeIdentifier fileTypeIdentifier;

    FileType(String description, int id, FileTypeIdentifier fileTypeIdentifier) {
        this.description = description;
        this.id = (byte)id;
        this.fileTypeIdentifier = fileTypeIdentifier;
    }

    public byte id() { return id; }
    public FileTypeIdentifier fileTypeIdentifier() { return fileTypeIdentifier; }
    public String description() { return description; }

    public static int indexOf(FileType ft) {
        for(int i=0;i<values().length;i++) {
            if (ft == values()[i]) return i;
        }
        return -1;
    }

    public static FileType getFileTypeById(int id) {
        for(int i=0;i<values().length;i++) {
            if (id == values()[i].id) return values()[i];
        }
        return null;
    }

    public String toString() {
        return "FileType "+description+" ("+id+")";
    }

    public static FileType getByFileName(String filename) {
        for(FileType t : values()) {
            if (t == EVERYTHING) continue;
            if (t.fileTypeIdentifier().matches(filename)) return t;
        }
        return EVERYTHING;
    }
}
